/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PCR;

import graphics.GraphicsEngin;
import java.awt.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JSlider;
import javax.swing.table.DefaultTableModel;
import popups.PopupFormManager;
import popups.PopupFrame;
import popups.ToolActionCommander;

/**
 *
 * @author marwa
 */
public class Toolbox extends javax.swing.JInternalFrame {
    
    DefaultTableModel markersModel;
    DefaultTableModel bandsModel;
    PopupFrame popUp = new PopupFrame();
    ToolActionCommander commander = new ToolActionCommander();

    /**
     * Creates new form Toolbox
     */
    public Toolbox() {
        initComponents();
       // opacity.setEnabled(false);
        bandsModel = PopupFormManager.getComponents("Band");
                GridLayout gLayout = new GridLayout(bandsModel.getRowCount() / 2, bandsModel.getRowCount() / 2);
                popUp.getContainer().setLayout(gLayout);
                commander.setBandsModel(bandsModel);
    }
    
    private void getBands() {
        popUp.getContainer().removeAll();
        for (int i = 0; i < bandsModel.getRowCount() - 1; i++) {
            String bandType = (String) bandsModel.getValueAt(i, 0);
            String bandIamge = (String) bandsModel.getValueAt(i, 1);

            JButton btn = new JButton();
            
            commander.setToolTitle("Bands");
            btn.addMouseListener(commander);
            Icon icn = new javax.swing.ImageIcon("resources/bands/" + bandIamge);
            Image img = ((ImageIcon) icn).getImage();
                
            ImageIcon icon = new ImageIcon(img.getScaledInstance(70, 30, Image.SCALE_AREA_AVERAGING));
            btn.setIcon(icon);
            btn.getAccessibleContext().setAccessibleDescription(Integer.toString(i));
            btn.setToolTipText(bandType);

            Dimension dim = new Dimension(50, 30);
            popUp.getContainer().add(btn);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        opacity = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Toolbox");

        opacity.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                opacityStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N
        jLabel1.setText("Opacity");

        jButton1.setText("Bands");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(opacity, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(0, 82, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 276, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(opacity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48))
        );

        jButton1.getAccessibleContext().setAccessibleName("Band");

        getAccessibleContext().setAccessibleName("Band");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void opacityStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_opacityStateChanged

        
        JSlider source = (JSlider)evt.getSource();
    if (!source.getValueIsAdjusting()) {
        int opacityValue = (int)source.getValue();
        MainFrame frm = (MainFrame)java.awt.Window.getWindows()[0];      
       
       GraphicsEngin.changeBackground(frm.getPCR().getWorkArea().getBandsPanel(), opacityValue) ;
       
    }
    }//GEN-LAST:event_opacityStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        JButton curBtn = (JButton) evt.getSource();
        PointerInfo a = MouseInfo.getPointerInfo();
        Point pt = a.getLocation();
        if (curBtn.getAccessibleContext().getAccessibleName().equals("Band")) {
            getBands();
        }

        popUp.setLocation(pt.x, pt.y);
        popUp.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSlider opacity;
    // End of variables declaration//GEN-END:variables
}
